#include <iostream>
namespace lzb
{
    typedef int ADT;

    // 链表中的数据节点
    typedef struct Node
    {
        ADT data;          // 数据域
        struct Node *next; // 指向下一个节点的指针
        Node(ADT value) : data(value), next(nullptr)
        {
        }
    } node;

    class list
    {
    public:
        list() : _head(nullptr), _count(0)
        {
        }
        ~list() {}

    public:
        // 尾插
        void push_back(const ADT &val)
        {
            Node *newnode = new Node(val);
            if (!_head)
                _head = newnode; // 没有头结点就创建一个
            else
            {
                Node *current = _head;
                while (current->next)
                    current = current->next; // 找到尾结点并插入
                current->next = newnode;
            }
            _count++;
        }
        // 遍历打印链表
        void show_list() const
        {
            Node *current = _head;
            while (current)
            {
                std::cout << current->data << "->";
                current = current->next;
            }
            std::cout << "NULL" << std::endl;
        }
        // 清空链表
        void clear()
        {
            Node *current = _head;
            while (current)
            {
                Node *temp = current;
                current = current->next;
                delete temp;
            }
            _head = nullptr;
            _count = 0;
        }
        // 获取链表节点个数
        int size() const
        {
            return _count;
        }

    private:
        Node *_head; // 链表头结点指针
        int _count;  // 链表数据节点的个数
    };
}

int main()
{
    lzb::list list;
    srand(time(nullptr));
    for (size_t i = 0; i < 10; i++)
    {
        list.push_back(rand() % 100); // 插入10个100以内的随机数进行测试
    }
    list.show_list();
    std::cout << "list.size(): " << list.size() << std::endl;
    list.clear();
    list.show_list();
    std::cout << "list.size(): " << list.size() << std::endl;
    return 0;
}
